package com.example.demo01.demo;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.ToastUtil;

public class PopupDemo extends AppCompatActivity {
    ToastUtil toastUtil = new ToastUtil(this);

    LinearLayout popup_window_demo;

    Button btn_popup1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.PopupDemo)))
                .commit();

        initView();
    }

    void initView() {
        /// 弹窗
        btn_popup1 = findViewById(R.id.btn_popup1);
        popup_window_demo = findViewById(R.id.main);

        btn_popup1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup();
            }
        });

    }

    private void showPopup() {
        View view = LayoutInflater
                .from(this)
                .inflate(R.layout.style_popup_window, null);

        TextView tv_photo = view.findViewById(R.id.tv_photo);
        TextView tv_album = view.findViewById(R.id.tv_album);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);

        PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //. 设置宽度
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); //. 设置高度
        popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.分割线_1, null))); //. 设置背景
        popupWindow.setContentView(view); //. 设置界面
        popupWindow.setTouchable(true); //. true时界面可点
        popupWindow.setFocusable(true); //. true时PopupWindow处理返回键
        popupWindow.setOutsideTouchable(true); //. true时点击外部消失，如果touchable为false时，点击界面也消失
        //! 这里注意 showAtLocation 方法,不要联想到showAsDropDown了
        popupWindow.showAtLocation(popup_window_demo, Gravity.BOTTOM, 0, 0); //. 显示到哪里

        tv_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastUtil.showToast("打开相机");
                popupWindow.dismiss();
            }
        });

        tv_album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastUtil.showToast("打开相册");
                popupWindow.dismiss();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastUtil.showToast("关闭弹窗");
                popupWindow.dismiss();
            }
        });
    }

}